package com.carmeth.geo;

import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

import com.carmeth.geo.bean.Casa;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

public class ListadoCasas extends ListActivity {
	
	private final String CLASS=this.getClass().getSimpleName();
	
	private String XML_URL = "http://192.168.1.72:8085/housesAdmin/houseX/casas.xml";
	private ArrayList<Casa> casas;
	
	/** Called when the activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listado);
        
        SAXParserFactory spf = SAXParserFactory.newInstance();
        CasaHandler jh = new CasaHandler();
        SAXParser sp;
		try {
			sp = spf.newSAXParser();
			sp.parse(XML_URL, jh);
			casas = jh.getCasas();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(casas == null){
				casas = new ArrayList<Casa>();
			}
		}
		
	    ListView lv = (ListView)findViewById(android.R.id.list);
	    lv.setAdapter(new CasasListAdapter(casas, getApplicationContext()));
    }
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		
		Intent mi = new Intent(getApplicationContext(), InfoTab.class);
    	mi.putExtra("casa", casas.get(position));
    	
    	startActivity(mi);
	}
}
